package extend_widget

import (
	"clientDemo/ui/fyne_ui/widget/interface"
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/driver/desktop"
	"github.com/ian-kent/go-log/log"
)

type Action struct {
	base     fyne.Widget              //控件主体
	rootNode widgetcommon.IWidgetItem //控件上层节点(容器)
}

func (a *Action) SetBase(wid fyne.Widget, rootNode widgetcommon.IWidgetItem) {
	a.base = wid
	a.rootNode = rootNode
}
func (a *Action) GetParent() widgetcommon.IWidgetItem {
	return a.rootNode
}
func (a *Action) GetBase() fyne.Widget {
	return a.base
}
func (a *Action) MouseDown(ev *desktop.MouseEvent) {
	//log.Println("[Action]down                %v  %v", ev.AbsolutePosition, ev.Position)
	parentPos := a.base.(fyne.CanvasObject).Position()
	// relativePos 点击当前控件相对上层容器内的坐标
	relativePos := fyne.NewPos(parentPos.X+ev.Position.X, parentPos.Y+ev.Position.Y)
	if a.rootNode == nil {
		return
	}
	a.rootNode.(widgetcommon.IEmptyWidgetTransEvent).TransMouseDown(ev, relativePos, a.base.(widgetcommon.IWidgetItem))
}
func (a *Action) MouseUp(ev *desktop.MouseEvent) {
	//log.Println("[Action]up                %v  %v", ev.AbsolutePosition, ev.Position)
	parentPos := a.base.(fyne.CanvasObject).Position()
	// relativePos 点击当前控件相对上层容器内的坐标
	relativePos := fyne.NewPos(parentPos.X+ev.Position.X, parentPos.Y+ev.Position.Y)
	if a.rootNode == nil {
		return
	}
	a.rootNode.(widgetcommon.IEmptyWidgetTransEvent).TransMouseUp(ev, relativePos, a.base.(widgetcommon.IWidgetItem))
}
func (a *Action) MouseMoved(ev *desktop.MouseEvent) {
	log.Printf("[Action]MouseMoved %v  %v  Name:%v", ev.AbsolutePosition, ev.Position, a.base.(widgetcommon.IWidgetItem).GetName())
	parentPos := a.base.(fyne.CanvasObject).Position()
	// relativePos 点击当前控件相对上层容器内的坐标
	relativePos := fyne.NewPos(parentPos.X+ev.Position.X, parentPos.Y+ev.Position.Y)
	if a.rootNode == nil {
		a.base.(widgetcommon.IEmptyWidgetTransEvent).TransMouseMoved(ev, relativePos, a.base.(widgetcommon.IWidgetItem))
		return
	}
	log.Printf("[Action]MouseMoved-------------------------- %v  %v", ev.AbsolutePosition, ev.Position)
	a.rootNode.(widgetcommon.IEmptyWidgetTransEvent).TransMouseMoved(ev, relativePos, a.base.(widgetcommon.IWidgetItem))
}
func (a *Action) MouseIn(ev *desktop.MouseEvent) {
	//log.Printf("[Action]MouseIn %v  %v", ev.AbsolutePosition, ev.Position)
}
func (a *Action) MouseOut() {
	//log.Printf("[Action]MouseOut ")
}

//type Hoverable interface {
//	// MouseIn is a hook that is called if the mouse pointer enters the element.
//	MouseIn(*MouseEvent)
//	// MouseMoved is a hook that is called if the mouse pointer moved over the element.
//	MouseMoved(*MouseEvent)
//	// MouseOut is a hook that is called if the mouse pointer leaves the element.
//	MouseOut()
//}
